//
//  ActivityVC.m
//  ErWa
//
//  Created by 赵颜彬 on 16/3/10.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "ActivityVC.h"
#import "ActivityCell.h"
#import "ActivityModel.h"
#import <UIImageView+WebCache.h>
#import "AFNetworking.h"
#import "ActivityDetailpageVC.h"
#import "NewNestHeader.h"
@interface ActivityVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableview;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, assign) NSInteger number;/**< 用来保存加载的动态值 */
@end

@implementation ActivityVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"优选活动";
    
    [self dataHandler];
    
    [self createSubviews];
}

/// 数据处理
- (void)dataHandler
{
    NSString *urlStr = @"http://ihome.cmfmobile.com:8080/sp/custom/getGoodActivity.do?pn=1";
    [AFNetworking GETWithURL:urlStr completion:^(id result) {
        NSMutableArray *arr = result;
        self.dataArr = [ActivityModel modelHanderWithArray:arr];
        [_tableview reloadData];
    }];
}

/// 视图创建
- (void)createSubviews
{
    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    _tableview.delegate = self;
    _tableview.dataSource = self;
    _tableview.backgroundColor = [UIColor clearColor];
    [_tableview registerClass:[ActivityCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_tableview];
}

// 设置cell个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}

// 设置cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ActivityCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    //消除点击后的灰色
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    // 判断当前是什么模式
    BOOL model = [AccountManager sharedInstance].isNight;
    if (model == YES) {
        cell.backgroundColor = [UIColor changeColorWithString:@"#a1a3a6"];
    } else {
        cell.backgroundColor = [UIColor clearColor];

    }
    
    if (_dataArr.count != 0) {
        ActivityModel *m = _dataArr[indexPath.row];
        cell.titleLabel.text = m.name;
        [cell.imgView sd_setImageWithURL:[NSURL URLWithString:m.logo] placeholderImage:[UIImage imageNamed:@"fang"]];
        cell.dateLabel.text = m.time;
        cell.addressLabel.text = m.areaDetail;
        return cell;
        
    }else{
        return cell;
    }
}

// 设置cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 170;
}
// 点击进去详情
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ActivityDetailpageVC *activityVC = [[ActivityDetailpageVC alloc] init];
    ActivityModel *m = _dataArr[indexPath.row];
    activityVC.modal = m;
    [self.navigationController pushViewController:activityVC animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
